package org.example.exeption;

import lombok.extern.slf4j.Slf4j;
import org.example.pojo.Result;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public Result handleException(Exception e) {

        log.info("服务器发生异常:{}", String.valueOf(e));
        return Result.error("出错了 ");
    }
    @ExceptionHandler
    public Result DuplicateKeyException(DuplicateKeyException e) {
        log.error("发生错误了00:", e);
        String message = e.getMessage();
        int i = message.indexOf("Duplicate entry");
        String errMsg = message.substring(i);
        String []arr = errMsg.split(" ");
        return Result.error(arr[2]+"，已存在");



    }
    @ExceptionHandler
    public Result handleException( RuntimeException e) {
        log.error("服务器发生异常:"+ e);
        return Result.error( e.getMessage());
    }




}
